package com.lib.index;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import com.lib.Serialize.objectSerializeUtil;

import com.lib.model.encryptedIndex;



public class buildIndex {
	/**
	 * 得到fileNameList内所有文件的加密的索引
	 * @param fileNameList
	 * @return
	 */
	public static Map<String, encryptedIndex> getAllEncryptedIndexs(List<String> fileNameList,indexEncrypted inSK)
	{
		Map<String, encryptedIndex>indexMap=new HashMap<String, encryptedIndex>();
		Iterator<String>iterator=fileNameList.iterator();
		encryptedIndex tempEnIndex=new encryptedIndex();
		while(iterator.hasNext())
		{
			String fileName=iterator.next();
			double []plainIndex=indexHelper.getSearchPlainIndex(fileName);
			tempEnIndex=inSK.IndexEncrypted(plainIndex);
			indexMap.put(fileName, tempEnIndex);
			//resultList.add(getNewEncryptedIndex(fileName));
		}
		return indexMap;
	}
	/**
	 * 得到文件fileName的加密的索引
	 * @param fileName
	 * @return
	 */
	public static encryptedIndex getNewEncryptedIndex(String fileName)
	{
		encryptedIndex resultEnIndex=new encryptedIndex();
		double []plainIndex=indexHelper.getSearchPlainIndex(fileName);
		indexEncrypted inSK=getEncryptedSK();
		resultEnIndex=inSK.IndexEncrypted(plainIndex);
		return resultEnIndex;
	}

	/**
	 * 得到一个秘钥三元组，如果已经为该数据拥有者生成了SK，那么直接从文件中读取，否者为
	 * 新的数据拥有者生成一个新的SK.
	 * @return
	 */
	public static indexEncrypted getEncryptedSK()
	{
		indexEncrypted resultSK=null;
		File file=new File("SK.obj");
		if(file.exists()&&file.length()!=0)
		{
			try {
				resultSK=(indexEncrypted) objectSerializeUtil.deserialize("SK.obj");
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		else {
			resultSK=new indexEncrypted();
			try {
				objectSerializeUtil.serialize(resultSK, "SK.obj");
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return resultSK;
	}
	

}
